﻿using IM.Easy.Core.Enums;
using IM.Easy.Core.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IM.Easy.Core.Utils
{
    public static class ExceptionUtils
    {
        /// <summary>
        /// 抛出业务异常
        /// </summary>
        /// <param name="message">异常消息</param>
        /// <exception cref="BusinessException">业务异常</exception>
        public static void ThrowBusinessException(string message)
        {
            if (string.IsNullOrEmpty(message))
                throw new ArgumentException("异常消息不能为空", nameof(message));

            throw new BisException(message);
        }

        /// <summary>
        /// 抛出系统异常
        /// </summary>
        /// <param name="message">异常消息</param>
        /// <exception cref="SystemException">系统异常</exception>
        public static void ThrowSystemException(string message)
        {
            if (string.IsNullOrEmpty(message))
                throw new ArgumentException("异常消息不能为空", nameof(message));

            throw new SystemException(message);
        }

        /// <summary>
        /// 抛出参数异常
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="message">异常消息</param>
        /// <exception cref="ArgumentException">参数异常</exception>
        public static void ThrowArgumentException(string paramName, string message)
        {
            if (string.IsNullOrEmpty(paramName))
                throw new ArgumentException("参数名不能为空", nameof(paramName));

            if (string.IsNullOrEmpty(message))
                throw new ArgumentException("异常消息不能为空", nameof(message));

            throw new ArgumentException(message, paramName);
        }

        /// <summary>
        /// 抛出无效操作异常
        /// </summary>
        /// <param name="message">异常消息</param>
        /// <exception cref="InvalidOperationException">无效操作异常</exception>
        public static void ThrowInvalidOperationException(string message)
        {
            if (string.IsNullOrEmpty(message))
                throw new ArgumentException("异常消息不能为空", nameof(message));

            throw new InvalidOperationException(message);
        }

        /// <summary>
        /// 抛出授权异常
        /// </summary>
        public static void ThrowUnauthorized()
        {
            throw new UnauthorizedAccessException("未授权");
        }

        /// <summary>
        /// 抛出自定义异常
        /// </summary>
        /// <param name="message">异常消息</param>
        /// <param name="exceptionType">异常类型</param>
        /// <exception cref="Exception">自定义异常</exception>
        public static void ThrowCustomException(string message, Type exceptionType)
        {
            if (string.IsNullOrEmpty(message))
                throw new ArgumentException("异常消息不能为空", nameof(message));

            if (exceptionType == null)
                throw new ArgumentNullException(nameof(exceptionType));

            if (!typeof(Exception).IsAssignableFrom(exceptionType))
                throw new ArgumentException("指定的类型不是异常类型", nameof(exceptionType));

            var exception = (Exception)Activator.CreateInstance(exceptionType, message);
            throw exception;
        }


        public static ResultCode GetStatusCode<TException>() where TException : Exception
        {
            return GetStatusCode(typeof(TException));
        }

        public static ResultCode GetStatusCode(Type exceptionType)
        {
            if (ExceptionCodeMap.ExceptionStatusCodes.TryGetValue(exceptionType, out var statusCode))
            {
                return statusCode;
            }

            // 默认状态码
            return ResultCode.InternalServerError;
        }


    }
}
